//
//  GuideListViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/8/26.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "GuideListViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "ProprotionViewController.h"
#import "GuideCell.h"
@interface GuideListViewController ()<UISearchBarDelegate,ProprotionViewControllerDelegate>

@end
@implementation GuideListViewController

{
    
    NSMutableArray*staffIdArray;
    NSMutableArray*staffNameArray;
    NSMutableArray*proprotionArray;
    UITableView*table;
    UIView*heardView;
    UILabel*how;
    NSString*str;
    
}
@synthesize mDelegate;

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
}


-(void)DeliveryType:(NSString *)DeliveryType DeliveryTypeCode:(NSString *)DeliveryTypeCode{

    [table removeFromSuperview];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-48) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    [proprotionArray replaceObjectAtIndex:str.intValue withObject:DeliveryTypeCode];
    double d;
    double d1;
    d=[[NSString stringWithFormat:@"%@",  [proprotionArray valueForKeyPath:@"@sum.floatValue"]]doubleValue];
    d1=[[NSString stringWithFormat:@"%@",  proprotionArray[0]]doubleValue];

    [proprotionArray replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%.f",  10-(d-d1)]];

    [table reloadData];
}




-(void)viewDidLoad{
    
    [super viewDidLoad];


    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"分佣";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-40) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
    staffIdArray=[[NSMutableArray alloc]init];
    staffNameArray=[[NSMutableArray alloc]init];
    proprotionArray=[[NSMutableArray alloc]init];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
     [param setValue:@"2" forKey:@"filterType"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Guide/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
       
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                for (NSDictionary* dicc in [severDataDict.dataDict objectForKey:@"DataLine"] )
                {
                   
                        [staffIdArray addObject:[dicc valueForKey:@"staffId"]];
                        [staffNameArray addObject:[dicc valueForKey:@"staffName"]];
                     if ([_selectedStaffIdArray containsObject:[dicc valueForKey:@"staffId"]]) {
                   
                         for (int i=0; i<_selectedStaffIdArray.count; i++) {
                             if ([[NSString stringWithFormat:@"%@",[dicc valueForKey:@"staffId"]]isEqualToString:    _selectedStaffIdArray[i]]) {
                                 [proprotionArray addObject:_selectedcodeArray[i]];

                                }
                             
                         }
                     }else{
                     
                          [proprotionArray addObject:@"0"];
                     }
                    
                    
                    }
                
                [table reloadData];
            }else{
                [Global hideProgressHUDInternal:self.view];
                
            }
            
        }
        else
        {
           
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];


    [table reloadData];
    
    
    
    UIButton* registed = [[UIButton alloc] initWithFrame:CGRectMake(0,ScreenHeight-48 ,ScreenWidth, 48)];
    [registed setTitle:@"确认" forState:UIControlStateNormal];
    registed.backgroundColor=luseColor;
    [registed setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registed.titleLabel.font=[UIFont systemFontOfSize:16];
    [registed addTarget:self action:@selector(registed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registed];
    

    
}
-(void)registed:(UIButton*)sender{
    
    
    NSMutableArray *selectCodeNameArray=[NSMutableArray array];
    NSMutableArray *selectIdNameArray=[NSMutableArray array];
    NSMutableArray *selectNameArray=[NSMutableArray array];
    for (int i=0; i<staffIdArray.count; i++) {
        if ([[NSString stringWithFormat:@"%@",[proprotionArray objectAtIndex:i]]intValue]>0) {
            [selectNameArray addObject:[staffNameArray objectAtIndex:i]];
            [selectIdNameArray addObject:[staffIdArray objectAtIndex:i]];
            [selectCodeNameArray addObject:[proprotionArray objectAtIndex:i]];

        }else{
        
        
        
            
        }
  
    }

    
    NSLog(@"------%@",staffNameArray);
     NSLog(@"------%@",proprotionArray);
    if (selectCodeNameArray.count>0) {
        [mDelegate selectCodeNameArray:selectCodeNameArray selectNameArray:selectNameArray selectIdNameArray:selectIdNameArray];

    }else{
    }
    
[self.navigationController popViewControllerAnimated:YES];

    
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [staffNameArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    GuideCell* cell = (GuideCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"GuideCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 55-0.40, ScreenWidth-20, 0.40)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   
    if (indexPath.section == 0) {
        cell.staffName.text = [staffNameArray objectAtIndex:indexPath.row] ;
    }
    cell.staffName.font=[UIFont systemFontOfSize:16];
    cell.codeLabel.font=[UIFont systemFontOfSize:14];
    cell.codeLabel.textAlignment=NSTextAlignmentRight;
    cell.codeLabel.textColor=shenhuiColor;
   
    if (indexPath.row == 0) {
        double d;
        double d1;
        d=[[NSString stringWithFormat:@"%@",  [proprotionArray valueForKeyPath:@"@sum.floatValue"]]doubleValue];
        d1=[[NSString stringWithFormat:@"%@",  proprotionArray[0]]doubleValue];
        cell.codeLabel.text=[NSString stringWithFormat:@"%.f成",10-(d-d1)];
       }else{
     
        if ([[NSString stringWithFormat:@"%@",[proprotionArray objectAtIndex:indexPath.row]]isEqualToString:@"0"]) {
             cell.codeLabel.text=@" ";
       
        }else{
            cell.codeLabel.text= [NSString stringWithFormat:@"%@成",[proprotionArray objectAtIndex:indexPath.row]];
        
        
        }
        
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-40,12, 30, 30);
        jiantou.userInteractionEnabled=NO;
        [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];

    }
    
    
   

    
    
    
   
    
        
    
    
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath .row==0) {
        
    }else{
    str=[NSString stringWithFormat:@"%ld",indexPath.row];

    ProprotionViewController  *generateVC=[[ProprotionViewController alloc] init];
    generateVC.proprotionStr=proprotionArray[indexPath.row];
    generateVC.mDelegate=self;
        double d;
        double d1;
        d=[[NSString stringWithFormat:@"%@",  [proprotionArray valueForKeyPath:@"@sum.floatValue"]]doubleValue];
        d1=[[NSString stringWithFormat:@"%@",  proprotionArray[0]]doubleValue];

    generateVC.fixedStr=[NSString stringWithFormat:@"%.f",10-(d-d1)];
    [self.navigationController pushViewController:generateVC animated:YES];
    }

}

@end
